/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipti.br.tag.entity;

import org.ipti.br.tag.entity.enums.Reference;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author root
 */

@Entity
public class PersonageSchedule extends AbstractEntity{

    @Column(name="week_day")
    private int weekDay;

    @Temporal(TemporalType.TIME)
    @Column(name="initial_time")
    private Calendar initialTime;

    @Temporal(TemporalType.TIME)
    @Column(name="final_time")
    private Calendar finalTime;

    @ManyToOne
    @JoinColumn(name = "task_id")
    private Task task;

    @ManyToOne
    @JoinColumn(name = "discipline_id")
    private Discipline discipline;

    @ManyToOne
    @JoinColumn(name = "personage_id")
    private Personage personage;

    @ManyToOne
    @JoinColumn(name = "unity_id")
    private Unity unity;

    @Column(name="reference_id")
    private Long referenceID;

    @Enumerated(EnumType.ORDINAL)
    private Reference reference;
    
    public PersonageSchedule() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = ""
                    + ((this.getInitialTime() != null) ? CalendarUtil.getFormatedDate(this.getInitialTime()) : null)
                    + ((this.getFinalTime() != null) ? CalendarUtil.getFormatedDate(this.getFinalTime()) : null)
                    + ((this.getTask() != null) ? this.getTask().getId() : null)
                    + ((this.getDiscipline() != null) ? this.getDiscipline().getId() : null)
                    + ((this.getPersonage() != null) ? this.getPersonage().getId() : null)
                    + ((this.getUnity() != null) ? this.getUnity().getId() : null)
                    + this.getReferenceID();
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }

    public PersonageSchedule(int weekDay, Calendar initialTime, Calendar finalTime, Task task, Discipline discipline, Personage personage, Unity unity) {
        this.weekDay = weekDay;
        this.initialTime = initialTime;
        this.finalTime = finalTime;
        this.task = task;
        this.discipline = discipline;
        this.personage = personage;
        this.unity = unity;
    }

    public PersonageSchedule(int weekDay, Calendar initialTime, Calendar finalTime, Task task, Discipline discipline, Personage personage, Unity unity, Long referenceID, Reference reference) {
        this.weekDay = weekDay;
        this.initialTime = initialTime;
        this.finalTime = finalTime;
        this.task = task;
        this.discipline = discipline;
        this.personage = personage;
        this.unity = unity;
        this.referenceID = referenceID;
        this.reference = reference;
    }

    public Calendar getFinalTime() {
        return finalTime;
    }

    public void setFinalTime(Calendar finalTime) {
        this.finalTime = finalTime;
    }

    public Calendar getInitialTime() {
        return initialTime;
    }

    public void setInitialTime(Calendar initialTime) {
        this.initialTime = initialTime;
    }

    public Personage getPersonage() {
        return personage;
    }

    public void setPersonage(Personage personage) {
        this.personage = personage;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public Discipline getDiscipline() {
        return discipline;
    }

    public void setDiscipline(Discipline discipline) {
        this.discipline = discipline;
    }

    public int getWeekDay() {
        return weekDay;
    }

    public void setWeekDay(int weekDay) {
        this.weekDay = weekDay;
    }

    public Reference getReference() {
        return reference;
    }

    public void setReference(Reference reference) {
        this.reference = reference;
    }

    public Long getReferenceID() {
        return referenceID;
    }

    public void setReferenceID(Long referenceID) {
        this.referenceID = referenceID;
    }
}
